/*
 * FileName：ReplyAutoController.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smart.common.BaseCtrl;
import javax.annotation.Resource;

import com.smart.common.util.AjaxResult;
import com.smart.common.util.SessionUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smart.wx.wxcms.entity.MediaFiles;
import com.smart.wx.wxcms.entity.MsgNews;
import com.smart.wx.wxcms.entity.ReplyAuto;
import com.smart.wx.wxcms.service.MediaFileService;
import com.smart.wx.wxcms.service.MsgNewsService;
import com.smart.wx.wxcms.service.ReplyAutoService;

/**
 * 
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-25 09:40:03
 */
@Controller
@RequestMapping("/wxcms/replyauto")
public class ReplyAutoController extends BaseCtrl {
    @Resource
    private ReplyAutoService replyAutoService;
    @Autowired
    private MsgNewsService msgNewsService;
	@Autowired
	private MediaFileService mediaFileService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public AjaxResult list(ReplyAuto replyAuto) throws Exception {
        List<ReplyAuto> replyAutoList = replyAutoService.queryListByPage(replyAuto);
        JSONObject json=new JSONObject();
        if(replyAutoList!=null&&!replyAutoList.isEmpty()){
        	ReplyAuto one =replyAutoList.get(0);
        	json=(JSONObject)JSON.toJSON(one);
        	if(one.getMsgType().equals("news")){
        		List<MsgNews> listNews = msgNewsService.getByMediaId(one.getMediaId());
        		//判断数据库是否存在数据
        		if(listNews!=null&&!listNews.isEmpty()){
        			json.put("data", listNews.get(0));
        		}
        	}else{
        		if(!one.getMsgType().equals("text")){
        			MediaFiles file = new MediaFiles();
        			file.setMediaId(one.getMediaId());
//        			file.setAccount(WxMemoryCacheClient.getAccount());
        			MediaFiles media = mediaFileService.getFileBySou(file);
        			json.put("data", media);
        		}
        	}
        }
        return getResult(replyAuto, json);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info")
    public AjaxResult info(ReplyAuto replyAuto) throws Exception {
		ReplyAuto one =replyAutoService.queryObject(replyAuto);
        return AjaxResult.success(one);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public AjaxResult save(ReplyAuto replyAuto) throws Exception {
    	//数据库中只能有一条数据，多的删除
    	List<ReplyAuto> replyAutoList = replyAutoService.queryListByPage(replyAuto);
    	if(replyAutoList!=null&&!replyAutoList.isEmpty()){
    		for (int i = 0; i < replyAutoList.size(); i++) {
    			replyAutoService.delete(replyAutoList.get(i));
			}
    	}
    	replyAuto.setUserId(SessionUtil.getUserId());
			replyAutoService.insert(replyAuto);
        return AjaxResult.saveSuccess();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public AjaxResult update(ReplyAuto replyAuto) throws Exception {
    	//是文本的要把mediaid清空
    	if(replyAuto.getMsgType().equals("text")){
    		replyAuto.setMediaId("");
    	}
    	
    	replyAuto.setUserId(SessionUtil.getUserId());
			replyAutoService.update(replyAuto);
        return AjaxResult.updateSuccess();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(ReplyAuto replyAuto) throws Exception {
    	replyAutoService.delete(replyAuto);
        return AjaxResult.deleteSuccess();
    }

}
